﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Linq;
using System.Linq;
using DevExpress.XtraReports;
using DevExpress.XtraReports.UI;

namespace Exact_Extension
{
    public partial class ProformaInvoice : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        Common common;
        public ProformaInvoice(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            common = new Common(DS);
            //this.LoadData();
        }
        public ProformaInvoice(ExactDataContext _DS, List<ISHCMC_V_StudentListWithDebtor> ListStudent)
        {
            InitializeComponent();
            DS = _DS;
            common = new Common(DS);
            this.LoadDataPerStudent(ListStudent);
        }
        private void LoadData()
        {
           

            var querySCHY = from SchoolYear in DS.ISHCMC_SchoolYears
                            select SchoolYear;
            this.iSHCMCSchoolYearBindingSource.DataSource = querySCHY;

            this.lookUpEdit_SchoolYear.ItemIndex = 0;

            List<ISHCMC_V_StudentListWithDebtor> CollectionStudent = new List<ISHCMC_V_StudentListWithDebtor>();
            var queryStudent = from ListStudent in DS.ISHCMC_V_StudentListWithDebtors
                               select ListStudent;
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in queryStudent)
            {
                var queryInv = from Invoice in DS.ISHCMC_Invoices
                               where Invoice.StudentID == itemStudent.StudentID && Invoice.SchoolYear == this.lookUpEdit_SchoolYear.EditValue.ToString()
                               && Invoice.Status ==0
                                select Invoice;
                if (queryInv.Count() > 0)
                {
                    itemStudent.Invoicing = true;
                }
                else
                {
                    itemStudent.Invoicing = false;
                }
                    CollectionStudent.Add(itemStudent);
               
            }
            this.iSHCMCVStudentListWithDebtorBindingSource.DataSource = CollectionStudent;
 
        }
        private void LoadDataPerStudent(List<ISHCMC_V_StudentListWithDebtor> ListStudent)
        {
           
            var querySCHY = from SchoolYear in DS.ISHCMC_SchoolYears
                            select SchoolYear;
            this.iSHCMCSchoolYearBindingSource.DataSource = querySCHY;

            this.lookUpEdit_SchoolYear.ItemIndex = 0;

            List<ISHCMC_V_StudentListWithDebtor> CollectionStudent = new List<ISHCMC_V_StudentListWithDebtor>();
           
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                var queryInv = from Invoice in DS.ISHCMC_Invoices
                               where Invoice.StudentID == itemStudent.StudentID && Invoice.SchoolYear == this.lookUpEdit_SchoolYear.EditValue.ToString()
                               && Invoice.Status == 0
                               select Invoice;
                if (queryInv.Count() > 0)
                {
                    itemStudent.Invoicing = true;
                }
                else
                {
                    itemStudent.Invoicing = false;
                }
                CollectionStudent.Add(itemStudent);

            }
            this.iSHCMCVStudentListWithDebtorBindingSource.DataSource = CollectionStudent;

           

        }
        
        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }


   
   
   
    
       private void btnPrint_Click(object sender, EventArgs e)
       {
           if (lookUpEdit_SchoolYear.EditValue != null)
           {
               if (lookUpEdit_SchoolYear.EditValue.ToString().Length == 4)
               {
                   RootRpt rootrpt = new RootRpt();
                   rootrpt.CreateDocument();
                   foreach (int i in this.gridView_Student.GetSelectedRows())
                   {

                       ISHCMC_V_StudentListWithDebtor student = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(i);
                       if (student.Invoicing == true)
                       {
                           List<ISHCMC_ListInvoiceDetail> ListInvDetail = new List<ISHCMC_ListInvoiceDetail>();
                           var queryInv = from InvoiceList in DS.ISHCMC_Invoices
                                          where InvoiceList.StudentID == student.StudentID && InvoiceList.SchoolYear == lookUpEdit_SchoolYear.EditValue.ToString()
                                          && InvoiceList.Status == 0
                                          select InvoiceList;



                           foreach (ISHCMC_Invoice itemInv in queryInv)
                           {
                               if (itemInv.Status == 0)
                               {
                                   if (itemInv.SpecialDiscount.Value == true)
                                   {
                                       if (itemInv.Authorize.Value == true)
                                       {
                                           ListInvDetail.Add(common.GetInvoiceDetail(itemInv));
                                       }
                                   }
                                   else
                                   {
                                       if (itemInv.Itemcode == "COE")
                                       {
                                           if (common.CheckStudentAssignedCOE(itemInv.StudentID))
                                           {
                                               ListInvDetail.Add(common.GetInvoiceDetail(itemInv));
                                           }

                                       }
                                       else
                                       {
                                           ListInvDetail.Add(common.GetInvoiceDetail(itemInv));
                                       }
                                   }
                               }
                           }
                           if (ListInvDetail.Count() > 0)
                           {
                               if (student.School == "ISHCMC")
                               {
                                   ProformaInvRpt rpt = new ProformaInvRpt();
                                   ISHCMC_StudentPayment itemPaid = common.GetStudentPayment(student.StudentID);
                                   rpt.SetListInvoiceDetail(ListInvDetail,itemPaid,student);
                                   rpt.SetTitle("SCHOOL FEES FOR " + DateTime.Now.Year.ToString() + " / " + (DateTime.Now.Year + 1).ToString() + " ACADEMIC YEAR");
                                   rpt.SetAttention(common.GetStudentAttention(student.StudentID));
                                   rpt.CreateDocument();
                                   rootrpt.Pages.AddRange(rpt.Pages);
                                   rootrpt.PrintingSystem.ContinuousPageNumbering = true;
                               }
                               if (student.School == "AAVN")
                               {
                                   ProformaInvAARpt rpt = new ProformaInvAARpt();
                                   ISHCMC_StudentPayment itemPaid = common.GetStudentPayment(student.StudentID);
                                   rpt.SetListInvoiceDetail(ListInvDetail,itemPaid);
                                   rpt.SetTitle("SCHOOL FEES FOR " + DateTime.Now.Year.ToString() + " / " + (DateTime.Now.Year + 1).ToString() + " ACADEMIC YEAR");
                                   rpt.SetAttention(common.GetStudentAttention(student.StudentID));
                                   rpt.CreateDocument();
                                   rootrpt.Pages.AddRange(rpt.Pages);
                                   rootrpt.PrintingSystem.ContinuousPageNumbering = true;
                               }
                           }

                       }
                      
                   }
                   ReportPrintTool PrintTool = new ReportPrintTool(rootrpt);
                   PrintTool.ShowPreview();
               }
               else
               {
                   MessageBox.Show("Please choose School Year !!");
               }
           }
           else
           {
               MessageBox.Show("Please choose School Year !!");
           }
       }

       private void btnRefresh_Click(object sender, EventArgs e)
       {
           this.LoadData();
       }

       private void gridView_Student_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
       {
           if (e.Clicks == 2)
           {
               
                ISHCMC_V_StudentListWithDebtor student = (ISHCMC_V_StudentListWithDebtor) this.gridView_Student.GetRow(e.RowHandle);
               var queryInvHeader = from InvoiceH in DS.ISHCMC_InvoiceHeaders
                                    where InvoiceH.Debcode == student.DebtorCode
                                    && InvoiceH.StudentID == student.StudentID
                                    && InvoiceH.SchoolYear == this.lookUpEdit_SchoolYear.EditValue.ToString()
                                    select InvoiceH;
               if (queryInvHeader.Count() > 0)
               {
                   ISHCMC_InvoiceHeader itemInvH = queryInvHeader.First();

                   InvoiceDetail inv = new InvoiceDetail(DS, itemInvH);
                   inv.Show();
               }
               else
               {
                   MessageBox.Show("This student haven't generate invoice yet");
               }
           }
       }

       private void btnImportInvoice_Click(object sender, EventArgs e)
       {
          
       }

       private void btnPrintLabel_Click(object sender, EventArgs e)
       {
           StudentAddressLabel rpt = new StudentAddressLabel(DS);
           rpt.ShowPreview();
       }


        
    }
}